﻿using LS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LS.ModelLogicHelpers {
    public class LostFoundHistoryLogicHelper {
        private LSDbContext db = new LSDbContext();

        public MemberCard GetMemberCardBySerialNumber(string SerialNumber) {
            var card = db.Cards.Where(item => item.SeiralNumber == SerialNumber).FirstOrDefault();
            var rtn = db.MemberCards.Where(item => item.CardId == card.Id).First();
            return rtn;
        }

        public bool Found(string SerialNumber, string FinderPhoneNumber) {
            var newRecord = new LostFoundHistory();
            var membercard = GetMemberCardBySerialNumber(SerialNumber);
            newRecord.MemberCardId = membercard.Id;
            newRecord.ReportDate = DateTime.Now;
            newRecord.UpdateDate = DateTime.Now;
            newRecord.ReportContact = FinderPhoneNumber;
            newRecord.Status = 0;
            db.LostFoundHistories.Add(newRecord);
            return db.SaveChanges() > 0;
        }

        public FoundResultModel GetResult(int MemberCardId) 
        {
            var rtn = new FoundResultModel();
            var membercard = db.MemberCards.Find(MemberCardId);
            if (membercard != null) 
            {
                var lfh=db.LostFoundHistories.Where(item => item.MemberCardId == membercard.Id && item.Status == 0).FirstOrDefault();
                rtn.ContactContent = lfh.ReportContact;
                rtn.Description = membercard.Description;
            }
            return rtn;
        }
    }
}